#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "pginstca.h"
#include "lang.h"

static LANGID language=1033;

/* from msiquery.h */
UINT WINAPI MsiGetPropertyA(
  MSIHANDLE hInstall,
  LPCTSTR szName,
  LPTSTR szValueBuf,
  DWORD* pchValueBuf
);


void pginst_settext(MSIHANDLE h)
{
	LANGID l;
	char tmp[32];
	DWORD szbuf = sizeof(tmp);

	ZeroMemory(tmp,sizeof(tmp));
	if (MsiGetPropertyA(h,"UILANG", tmp, &szbuf) != ERROR_SUCCESS)
	{
		language = 1033;
		return;
	}

	l = atoi(tmp);
	if (l <= 0)
		language = 1033;
	else
		language = l;
}

void pginst_settext2(char *langid)
{
	LANGID l;

	l = atoi(langid);
	if (l <= 0)
		language = 1033;
	else
		language = l;
}

WCHAR *pginst_gettext(const WCHAR *t)
{
	int i;
	
	if (language == 1033)
		return (WCHAR *)t;

	/* First try to find our pointer */
	for(i=0;;i++)
	{
		if (transmap[i].langid <= 0 || transmap[i].strings == NULL)
			/* No translation found */
			return (WCHAR *)t;
		if (transmap[i].langid == language)
		{
			/* Match in this language! Now search for the string */
			int j;
			for (j=0;;j++)
			{
				if (trans_base[j] == NULL)
				{
					/* No translation found. This case should never happen,
					   but just in case.. */
					return (WCHAR *)t;
				}
				if (!wcscmp(t, trans_base[j]))
				{
					/* Found at this index */
					if (transmap[i].strings[j] == NULL)
						/* This string was not translated */
						return (WCHAR *)t;
					else
						/* Return the actual translated string! */
						return (WCHAR *)transmap[i].strings[j];
				}
			}
		}
	}
}
